<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" :before-close="handleClose" :close-on-click-modal="false" width="450px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item :label="$t('deptName')" prop="departName">
          <el-input v-model="form.departName"/>
        </el-form-item>
        <el-form-item :label="$t('selectParent')">
          <treeselect
            :options="options"
            v-model="form.parentId"
            :placeholder="`${$t('pleaseSelect')}${$t('selectParent')}`"
            :no-children-text="$t('noMoreData')"
            :no-options-text="$t('noMoreData')"
            :no-results-text="$t('noMoreData')"
          />
        </el-form-item>
        <el-form-item :label="$t('departmentCategory')" prop="category">
          <el-select
            v-model="form.category"
            :placeholder="`${$t('pleaseInput')}${$t('departmentCategory')}`"
            style="width: 100%"
          >
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="`${$t(item.name)}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('description')" prop="desc">
          <el-input v-model="form.desc" rows="3" type="textarea" maxlength="200" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="handleClose">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{
          $t("save")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/system/dept'
import mySetting from '@/mixins/mySetting'
import enumApi from '@/api/system/enum'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { getTreeList } from '@/api/system/dept'

export default {
  name: 'DeptModel',
  components: { Treeselect },
  mixins: [mySetting],
  data() {
    return {
      dialogFormVisible: false,
      title: '',
      form: {},
      options: [],
      categoryList: [],
      rules: {
        departName: [
          { required: true, message: `${this.$t('pleaseInput')}${this.$t('deptName')}`, trigger: 'blur' }
        ],
        category: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('departmentCategory')}`, trigger: 'change' }]
      } // 表单校验规则
    }
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then(res => {
            this.$betterTips(res, this.handleUpdate, this.handleClose)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.$betterTips(res, this.handleUpdate, this.handleClose)
          })
        }
      })
    },
    // 打开弹窗
    async handleOpen(row) {
      this.getDeptList()

      if (row && row.id) {
        this.title = this.$t('modify')
        crudJob.get({ id: row.id }).then((res) => {
          this.form = Object.assign({}, res.data, { parentId: res.data.parentId ? res.data.parentId : undefined })
        })
      } else {
        this.title = this.$t('add')
      }
      this.dialogFormVisible = true
      const data = await getTreeList()
      const formatTree = (root) => {
        return (root || []).map(node => {
          const { id, name, children } = node
          return {
            id,
            label: name,
            children: formatTree(children)
          }
        })
      }
      this.options = formatTree(data.data)
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    // 触发父组件更新
    handleUpdate() {
      this.$emit('initList')
    },
    async getDeptList() {
      const res = await enumApi.getList({ enumName: 'DepartmentEnum' })
      this.categoryList = res.data
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .vue-treeselect__input-container{
  display: flex;
  align-items: center;
}
</style>
